package com.hoapital.outpbillwork.billWorkHandler;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hoapital.outpbillwork.module.entity.*;
import com.hoapital.outpbillwork.module.mapper.*;
import com.hospital.common.core.constants.AllFinalValueUtils;
import com.hospital.common.core.exception.ExceptionHelp;
import com.hospital.common.core.exception.ExceptionHelpEnum;
import com.hospital.common.core.module.controller.dto.BillDto;
import com.hospital.common.core.module.controller.dto.ClinicType;
import com.hospital.common.core.module.controller.vo.Receive;
import com.hospital.common.core.utils.SpringContextHolder;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lvyao
 * @description 计费及开单基础方法类
 * @date 2022/2/24
 */
@Data
abstract public class AbstractBaseBusiness<T extends BaseRequest> {
    @Autowired
    private OutpPrepayRcptTempMapper outpPrepayRcptTempMapper = SpringContextHolder.getBean(OutpPrepayRcptTempMapper.class);

    @Autowired
    private OrderVsBillnoMapper orderVsBillnoMapper = SpringContextHolder.getBean(OrderVsBillnoMapper.class);

    @Autowired
    private OutpOrderDescTempMapper outpOrderDescTempMapper = SpringContextHolder.getBean(OutpOrderDescTempMapper.class);

    @Autowired
    private AppConfigerBaseinfoMapper appConfigerBaseinfoMapper = SpringContextHolder.getBean(AppConfigerBaseinfoMapper.class);
    @Autowired
    private PatientInfoMapper patientInfoMapper = SpringContextHolder.getBean(PatientInfoMapper.class);
    @Autowired
    private OutpMedrecMapper outpMedrecMapper = SpringContextHolder.getBean(OutpMedrecMapper.class);

    /**
     * 数据校验全门诊业务接口通用
     *
     * @param requestVal
     * @return
     */
    protected ExceptionHelp CheckValue(T requestVal) throws ExceptionHelp {
        String toString = requestVal.toString();
        BaseRequest baseRequest = JSON.parseObject(toString, BaseRequest.class);
        if (baseRequest.getHospitalCode() == null || baseRequest.getHospitalName() == null || baseRequest.getOutpOrInp() == null ||
                baseRequest.getHospitalCode().trim().equals("") || baseRequest.getHospitalName().trim().equals("") ||
                baseRequest.getOutpOrInp().trim().equals("")) {
            return new ExceptionHelp(ExceptionHelpEnum.CHECK_INTERFACE_COMM);
        }
        return null;
    }


    /**
     * HL7消息转换
     */
    public ExceptionHelp changeMessage() throws ExceptionHelp {
        return null;
    }


    public BillDto getBillDto(String chargeType, Receive receive, List<ClinicType> clinicTypeList, String patientId, String outpId) throws ExceptionHelp {
        //组装收费信息模板计费
        BillDto billDto = new BillDto();
        billDto.setOutpOrInp(receive.getOutpOrInp());
        billDto.setHospitalCode(receive.getHospitalCode());
        billDto.setSettleFlag(receive.getSettleFlag());
        billDto.setHospitalName(receive.getHospitalName());
        billDto.setSettlePatientInfo(receive.getSettlePatientInfo());
        billDto.setChargeType(chargeType);
        billDto.setClinicTypeList(clinicTypeList);
        billDto.setPatientId(patientId);
        billDto.setOutpId(outpId);
        return billDto;
    }


    /**
     * 获取订单号对应的HIS业务单号
     *
     * @param orderNo
     * @return
     * @throws ExceptionHelp
     */
    protected List<OrderVsBillno> orderVsBillnoList(String orderNo) throws ExceptionHelp {
        QueryWrapper<OrderVsBillno> orderVsBillnoQueryWrapper = new QueryWrapper<>();
        orderVsBillnoQueryWrapper.eq(AllFinalValueUtils.ORDER_NO_FIELD_NAME, orderNo);
        List<OrderVsBillno> orderVsBillnoList = new ArrayList<>();
        try {
            orderVsBillnoList = orderVsBillnoMapper.selectList(orderVsBillnoQueryWrapper);
            if (orderVsBillnoList == null || orderVsBillnoList.isEmpty()) {
                throw new ExceptionHelp(ExceptionHelpEnum.BILL_GET_ORDER_DETAIL);
            }
        } catch (Exception e) {
            throw new ExceptionHelp(ExceptionHelpEnum.BILL_GET_ORDER_DETAIL);
        }
        return orderVsBillnoList;
    }

    /**
     * 按程序名称以及参数名称获取配置对象
     *
     * @param appName
     * @param paraName
     * @return
     * @throws ExceptionHelp
     */
    protected AppConfigerBaseinfo getAppConfigerBaseinfo(String appName, String paraName) throws ExceptionHelp {
        List<AppConfigerBaseinfo> appConfigerBaseinfoList = appConfigerBaseinfoMapper.selectList(new QueryWrapper<AppConfigerBaseinfo>().
                apply(" app_name = '" + appName + "' and parameter_name = '" + paraName + "'"));
        if (appConfigerBaseinfoList == null || appConfigerBaseinfoList.size() != 1) {
            throw new ExceptionHelp(ExceptionHelpEnum.BILL_NO_GET_ERROR);
        }
        return appConfigerBaseinfoList.get(0);
    }

    protected Map<String, String> getHl7InterConfig(String getHl7LabMessage) {
        Map<String, String> map = new HashMap<>();
        QueryWrapper<AppConfigerBaseinfo> appConfigerBaseinfoQueryWrapper1 = new QueryWrapper<>();
        appConfigerBaseinfoQueryWrapper1.eq("app_name", "*");
        List<AppConfigerBaseinfo> appConfigerBaseinfoList = appConfigerBaseinfoMapper.selectList(appConfigerBaseinfoQueryWrapper1);
        String filePath = null;
        String postHl7Url = null;
        String xmlHl7Url = null;
        try {
            filePath = appConfigerBaseinfoList.stream().filter(appConfigerBaseinfo -> appConfigerBaseinfo.getParameterName().equals("XML_HL7_FILE")).collect(Collectors.toList()).get(0).getParainitValue();
            postHl7Url = appConfigerBaseinfoList.stream().filter(appConfigerBaseinfo -> appConfigerBaseinfo.getParameterName().equals(getHl7LabMessage.equals(AllFinalValueUtils.TEST) ? "WX_HL7_POST_URL" : "WX_HL7_ALL_URL_" + getHl7LabMessage)).collect(Collectors.toList()).get(0).getParainitValue();
            xmlHl7Url = appConfigerBaseinfoList.stream().filter(appConfigerBaseinfo -> appConfigerBaseinfo.getParameterName().equals("XML_HL7_URL")).collect(Collectors.toList()).get(0).getParainitValue();
        } catch (Exception e) {
            throw new ExceptionHelp(ExceptionHelpEnum.GET_HL7_CONFIGER_ERR);
        }
        map.put("filePath", filePath);
        map.put("postHl7Url", postHl7Url);
        map.put("xmlHl7Url", xmlHl7Url);
        return map;
    }

    protected Map<String, Object> getHl7ApplyBaseData(String oneKey, String patientId, String visitId) {
        Map<String, Object> objectMap = new HashMap<>();
        try {
            //获取门诊开单数据
            OutpMedrec outpMedrec = outpMedrecMapper.getInfo(patientId, visitId);
            QueryWrapper<OutpOrderDescTemp> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(AllFinalValueUtils.PATIENT_ID_FIELD_NAME, patientId).eq(AllFinalValueUtils.OUTP_VISIT_TIME, visitId);
            List<OutpOrderDescTemp> outpOrderDescTempList = outpOrderDescTempMapper.selectList(queryWrapper);

            OutpPrepayRcptTemp outpPrepayRcptTemp = outpPrepayRcptTempMapper.selectOne(new QueryWrapper<OutpPrepayRcptTemp>().eq("order_no", oneKey));
            PatientInfo patientInfo = patientInfoMapper.selectById(patientId);

            objectMap.put("outpMedrec", outpMedrec);
            objectMap.put("outpOrderDescTempList", outpOrderDescTempList);
            objectMap.put("outpPrepayRcptTemp", outpPrepayRcptTemp);
            objectMap.put("patientInfo", patientInfo);
        } catch (Exception e) {
            throw new ExceptionHelp(ExceptionHelpEnum.PRESC_CHECK_GET_ORDER_ERR);
        }
        return objectMap;
    }

}
